load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test", "rust_test_suite")
load("//bazel:defs.bzl", "rust_ic_bench")

package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
    # Keep sorted.
    "//rs/artifact_pool",
    "//rs/bitcoin/client",
    "//rs/bitcoin/consensus",
    "//rs/config",
    "//rs/consensus/certification",
    "//rs/consensus/cup_utils",
    "//rs/crypto",
    "//rs/crypto/sha2",
    "//rs/cycles_account_manager",
    "//rs/execution_environment",
    "//rs/http_endpoints/async_utils",
    "//rs/http_endpoints/metrics",
    "//rs/http_endpoints/nns_delegation_manager",
    "//rs/http_endpoints/public",
    "//rs/http_endpoints/xnet",
    "//rs/https_outcalls/client",
    "//rs/interfaces",
    "//rs/interfaces/certified_stream_store",
    "//rs/interfaces/registry",
    "//rs/interfaces/state_manager",
    "//rs/messaging",
    "//rs/monitoring/logger",
    "//rs/monitoring/metrics",
    "//rs/monitoring/pprof",
    "//rs/monitoring/tracing",
    "//rs/monitoring/tracing/jaeger_exporter",
    "//rs/monitoring/tracing/logging_layer",
    "//rs/protobuf",
    "//rs/registry/client",
    "//rs/registry/helpers",
    "//rs/registry/local_store",
    "//rs/registry/subnet_type",
    "//rs/replicated_state",
    "//rs/state_manager",
    "//rs/sys",
    "//rs/types/types",
    "//rs/xnet/payload_builder",
    "@crate_index//:clap",
    "@crate_index//:hex",
    "@crate_index//:libc",
    "@crate_index//:nix",
    "@crate_index//:num_cpus",
    "@crate_index//:pprof",
    "@crate_index//:prometheus",
    "@crate_index//:rand",
    "@crate_index//:regex",
    "@crate_index//:slog",
    "@crate_index//:tempfile",
    "@crate_index//:tikv-jemalloc-ctl",
    "@crate_index//:tikv-jemallocator",
    "@crate_index//:tokio",
    "@crate_index//:tokio-util",
    "@crate_index//:tracing",
    "@crate_index//:tracing-subscriber",
]

MACRO_DEPENDENCIES = []

NONMALICIOUS_DEPENDENCIES = [
    # Keep sorted.
    "//rs/consensus:consensus",
    "//rs/replica/setup_ic_network:setup_ic_network",
]

MALICIOUS_DEPENDENCIES = [
    # Keep sorted.
    "//rs/consensus:malicious_consensus",
    "//rs/replica/setup_ic_network:malicious_setup_ic_network",
]

DEV_DEPENDENCIES = [
    # Keep sorted.
    "//rs/interfaces/registry/mocks",
    "//rs/rust_canisters/canister_test",
    "//rs/test_utilities",
    "//rs/types/management_canister_types",
    "@crate_index//:assert_cmd",
    "@crate_index//:criterion",
    "@crate_index//:predicates",
    "@crate_index//:wat",
]

MACRO_DEV_DEPENDENCIES = []

ALIASES = {}

DATA = [
    "//rs/rust_canisters/stable_structures:stable_structures_canister",
]

ENV = {
    "STABLE_STRUCTURES_CANISTER_WASM_PATH": "$(rootpath //rs/rust_canisters/stable_structures:stable_structures_canister)",
}

rust_library(
    name = "replica_lib",
    srcs = glob(["src/**"]),
    aliases = ALIASES,
    crate_name = "ic_replica",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES + NONMALICIOUS_DEPENDENCIES,
)

rust_library(
    name = "malicious_replica_lib",
    testonly = True,
    srcs = glob(["src/**"]),
    aliases = ALIASES,
    crate_name = "ic_replica",
    proc_macro_deps = MACRO_DEPENDENCIES,
    version = "0.9.0",
    deps = DEPENDENCIES + MALICIOUS_DEPENDENCIES,
)

rust_binary(
    name = "replica",
    srcs = glob(["bin/replica/**"]),
    aliases = ALIASES,
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + NONMALICIOUS_DEPENDENCIES + [":replica_lib"],
)

rust_binary(
    name = "malicious_replica",
    testonly = True,
    srcs = glob(["bin/replica/**"]),
    aliases = ALIASES,
    crate_features = ["malicious_code"],
    proc_macro_deps = MACRO_DEPENDENCIES,
    deps = DEPENDENCIES + MALICIOUS_DEPENDENCIES + [":malicious_replica_lib"],
)

rust_test(
    name = "replica_test",
    aliases = ALIASES,
    crate = ":replica",
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    tags = [],
    deps = DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_test_suite(
    name = "replica_integration",
    srcs = glob(["tests/**/*.rs"]),
    aliases = ALIASES,
    data = [":replica"],
    proc_macro_deps = MACRO_DEPENDENCIES + MACRO_DEV_DEPENDENCIES,
    tags = [],
    deps = [":replica_lib"] + DEPENDENCIES + DEV_DEPENDENCIES,
)

rust_ic_bench(
    name = "calls_bench",
    testonly = True,
    srcs = ["benches/calls.rs"],
    data = DATA,
    env = ENV,
    deps = [
        # Keep sorted.
        "//rs/rust_canisters/canister_test",
        "@crate_index//:criterion",
    ],
)
